package org.shj.spark.streaming;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import org.apache.spark.broadcast.Broadcast;
import org.apache.spark.streaming.Durations;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.apache.spark.util.LongAccumulator;

import scala.Tuple2;

public class BroadcastAndAccumulator {
	
	private static volatile Broadcast<List<String>> bclist = null;
	private static volatile LongAccumulator accumulator = null;

	public static void main(String[] args) throws Exception{
		SparkConf conf = new SparkConf().setAppName("BroadcastAndAccumulator");
		conf.setMaster("local[*]");
		
		List<String> blackList = new ArrayList<String>();
		blackList.add("fuck");
		blackList.add("cao");
		
		JavaStreamingContext jsc = new JavaStreamingContext(conf, Durations.seconds(5));
		jsc.sparkContext().setLogLevel("WARN");
		
		bclist = jsc.sparkContext().broadcast(blackList);
		accumulator = jsc.sparkContext().sc().longAccumulator("blackListCnt");
		
		Queue<JavaRDD<String>> rddQueue = new LinkedList<>();
		
		generate(rddQueue, jsc.sparkContext());
		
		JavaDStream<String> queueStream = jsc.queueStream(rddQueue);
		
		JavaDStream<String> words = queueStream.flatMap(new FlatMapFunction<String, String>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Iterator<String> call(String t) throws Exception {
				return Arrays.asList(t.split("\\s+")).iterator();
			}
		});
		
		JavaPairDStream<String, Integer> wordCnt = words.mapToPair(new PairFunction<String, String, Integer>() {
			private static final long serialVersionUID = -3094129940146330892L;

			public Tuple2<String, Integer> call(String t) throws Exception {
				return new Tuple2<String, Integer>(t, 1);
			}
		}).reduceByKey(new Function2<Integer, Integer, Integer>() {
			private static final long serialVersionUID = -2097776848860283529L;

			@Override
			public Integer call(Integer v1, Integer v2) throws Exception {
				return v1 + v2;
			}
		});
		
		wordCnt.foreachRDD(new VoidFunction<JavaPairRDD<String,Integer>>() {
			private static final long serialVersionUID = -6661215922534603661L;

			@Override
			public void call(JavaPairRDD<String, Integer> rdd) throws Exception {
				List<Tuple2<String, Integer>> result = rdd.filter(new Function<Tuple2<String,Integer>, Boolean>() {
					private static final long serialVersionUID = 1L;

					@Override
					public Boolean call(Tuple2<String, Integer> v1) throws Exception {
						if(bclist.value().contains(v1._1)) {
							accumulator.add(v1._2);
							return false;
						}
						return true;
					}
				}).collect();
				
				for(Tuple2<String, Integer> t : result) {
					System.out.println("(" + t._1 +", "+ t._2+")");
				}
				System.out.println("accumulator: " + accumulator.value());
			}
		});
		
		jsc.start();
		jsc.awaitTermination();
		jsc.close();
	}
	
	private static void generate(Queue<JavaRDD<String>> queue, 
			JavaSparkContext sc) throws Exception{
		for(int i = 0 ; i < 10 ; i++) {
			synchronized(queue) {
				List<String> list = Arrays.asList("I fuck you", "I cao you");
				queue.add(sc.parallelize(list, 2));
			}
			Thread.sleep(1000);
		}
	}

}
